/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.dao.impl;

import com.tesam.gedeon.dao.UserDao;
import com.tesam.gedeon.entity.Country;
import com.tesam.gedeon.entity.SystemUser;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Sistema
 */
@Repository
public class UserDaoImpl extends SimpleJdbcDaoSupport implements UserDao {

    @Autowired
    public UserDaoImpl(DataSource dataSource) {
        setDataSource(dataSource);
    
    }
    
    @Override
    public List<SystemUser> getProfileById(SystemUser user) {
         List<SystemUser> result = new ArrayList();
        try{
            String sql = "select sys.*, co.name as countryName from systemuser sys "
                    + " inner join country co on sys.country = co.longcode  where sys.id = "+user.getId() ;
            System.out.println(sql);
            List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);
                for (Map row : rows) {
                    SystemUser userE = new SystemUser();
                    
                     userE.setId((Integer)(row.get("id"))); 
                     userE.setName((String)(row.get("name")));
                     userE.setPhone((String)(row.get("phone")));
                     userE.setEmail((String)(row.get("email")));
                     userE.setWebsite((String)(row.get("website")));
                     userE.setFax((String)(row.get("fax")));
                     userE.setImageName((String)(row.get("imagename")));
                     userE.setLastName((String)(row.get("lastname")));
                     userE.setPhoneHome((String)(row.get("phonehome")));
                     userE.setBirthDay((Date)(row.get("birthday")));
                     userE.setOtherPhone((String)(row.get("otherphone")));
                     userE.setEmail2((String)(row.get("email2")));
                     userE.setPhoneWork((String)(row.get("phonework")));
                     userE.setDocumentIdentity((String)(row.get("documentidentity")));
                     userE.setDocumentType((String)(row.get("documenttype")));
                     userE.setUserName((String)(row.get("username")));
                     
                     Country country = new Country();
                     country.setLongCode((String)(row.get("country")));
                     country.setName((String)(row.get("countryName")));
                     
                     userE.setCountry(country);
                     
//                     user.setRol(null);
                     
                      
                    result.add(userE);
                }
        }catch(Exception e){
            System.out.println("getProfileById : "+e.getMessage());
        }
        return result ;
        
    }
    
}
